// @charset "utf-8";

define(function(require, exports, module) {
	var publicMethod = require('./public');

	function ClientQuote() {
		this.quote = 'NEW, ZHANGDIEFU';
		this.period = 'now';
		this.init.apply(this, arguments);
	}
	ClientQuote.prototype = {

		init: function(params) {
			// var defaults = {
			// 	quote: 'NEW, ZHANGDIEFU';
			// 	period: 'now',
			// 	stocks: '300033,600000'
			// };
			this.stocks = params.stocks;
			this.quote = params.quote || this.quote;
			this.getQuote = params.getQuote || params.quote
			this.period = params.period || this.period;

			this.callback = params.callback;
			this.isNeedPush = params.isNeedPush || false;
			this.hqData = {};

			try {
				this.thsQuote = new ThsQuote();
			} catch (e) {}

		},
		request: function() {
			var self = this;
			var stocks = self.stocks,
				quote = self.quote;

			var reqParams = {
				code: stocks,
				type: quote,
				period: self.period,
				onready: async function(p) {
					await self.getQuoteData(stocks, quote);
				}
			};
			self.thsQuote.request(reqParams);
			if (self.isNeedPush) {
				self.resisterPush(stocks);
			}
		},
		resisterPush: function(stocks) {
			var self = this;
			stocks = stocks || self.stocks;
			self.thsQuote.registerPush({
				code: stocks,
				// onready: async function(p) {
				// 	await self.getQuoteData(stocks, quote);
				// }
				onready: publicMethod.throttle(self.getQuoteData, 10000, self)
			});
		},
		unregisterPush: function() {
			this.thsQuote.unregisterPush();
		},
		getQuoteData: async function(stocks, type) {
			var self = this;
			stocks = self.stocks;
			type = self.getQuote;

			var params = {
				code: stocks,
				mode: 'maexact',
				type: type
			};
			var rstData = await self.thsQuote.getData2(params);
			rstData = eval('(' + rstData + ')');
			self.callback(rstData);
		}
	};

	module.exports = ClientQuote;
});
